﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSimplicity.Data;
using System.Text;
using System.IO;
using WebSimplicity.Data.Helpers;
using System.Text.RegularExpressions;

namespace WebSimplicity.Admin.Controllers
{
    public class HomeController : Controller
    {
        static Website web = new Website().Current();
        static string theme = ShortGuid.Encode(web.Theme);
        //
        // GET: /Home/
        public ActionResult Index(string id, string extension)
        {
            switch (extension)
            {
                case "pg":
                case null:
                    var model = Pages.GetCurrent();
                    return View(model.Template, model);
                default:
                    return View();
            }
        }
        public ActionResult css(string id)
        {
            List<string> files = id.Split(',').ToList();
            StringBuilder sb = new StringBuilder();
            string body = string.Empty;
            StreamReader sr;
            foreach (string file in files)
            {
                sr = new StreamReader(Server.MapPath("~/Themes/"
                                + theme + "/css/" + file + ".css"));
                body = sr.ReadToEnd();
                sr.Close();
                body = Regex.Replace(body, @"[a-zA-Z]+#", "#");
                body = Regex.Replace(body, @"[\n\r]+\s*", string.Empty);
                body = Regex.Replace(body, @"\s+", " ");
                body = Regex.Replace(body, @"\s?([:,;{}])\s?", "$1");
                body = body.Replace(";}", "}");
                body = Regex.Replace(body, @"([\s:]0)(px|pt|%|em)", "$1");
                sb.AppendLine(body);   
            }
            return Content(sb.ToString(), "text/css");
        }
        public ActionResult js(string id)
        {
            List<string> files = id.Split(',').ToList();
            StringBuilder sb = new StringBuilder();
            string body = string.Empty;
            StreamReader sr; foreach (string file in files)
            {
                sr = new StreamReader(Server.MapPath("~/Themes/" +
                                   theme + "/js/" + file + ".js"));
                body = sr.ReadToEnd();
                sr.Close();
                sb.AppendLine(body);
            }
            return Content(sb.ToString(), "text/javascript");
        }
        public FileResult images(string id)
        {
            string contentType = string.Empty;
            string filepath = Server.MapPath("~/Themes/" + theme + "/images/" + id);
            switch (Path.GetExtension(Request.Url.ToString()))
            {
                case ".bmp": contentType = "Image/bmp";
                    break;
                case ".gif": contentType = "Image/gif";
                    break;
                case ".jpg": contentType = "Image/jpeg";
                    break;
                case ".png": contentType = "Image/png";
                    break;
                default: break;
            }
            return File(filepath, contentType);
        }
        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Home/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Delete/5
  
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
